import { defineStore } from 'pinia'
import cache from '@/utils/cache'
import { ECacheKey } from '@/config/enums'
import { USER_INFO } from '@/stores/constant/cacheKey'

import { login, logout } from '@/api/admin/system/user'

export interface UserState {
  token: string
  userInfo: Record<string, any>
  breadcrumb: any[]
  menu: any[]
  perms: string[]
}
const useUserStore = defineStore('useUserInfo', {
  state: (): UserState => {
    return {
      token: cache.get(ECacheKey.TOKEN) || '', // token
      userInfo: {}, // 用户信息
      breadcrumb: [],
      menu: [], // 权限
      perms: []
    }
  },
  getters: {},
  actions: {
    resetLoginInfo() {
      this.token = ''
      this.userInfo = {}
      this.menu = []
      this.perms = []
      cache.remove(ECacheKey.TOKEN)
    },
    // 更新头像
    setAvatar(avatarUrl: any) {
      this.userInfo.avatar = avatarUrl
    },
    // 登录
    login(playload: any) {
      const { userName, password } = playload
      return new Promise((resolve, reject) => {
        login({ userName, password })
          .then((data: any) => {
            this.userInfo = data.info
            this.token = `Bearer ${data.token}`
            cache.set(ECacheKey.TOKEN, `Bearer ${data.token}`)
            resolve(data)
          })
          .catch((error: any) => {
            reject(error)
          })
      })
    },
    // 退出登录
    logout() {
      return new Promise((resolve, reject) => {
        console.log('logout')
        logout()
          .then(async (data: any) => {
            resolve(data)
          })
          .catch((error: any) => {
            reject(error)
          })
      })
    },
    getUserInfo() {
      // return new Promise((resolve, reject) => {
      //   getUserInfo()
      //     .then((data) => {
      //       this.userInfo = data.user
      //       this.perms = data.permissions
      //       resolve(data)
      //     })
      //     .catch((error) => {
      //       reject(error)
      //     })
      // })
    },
    getMenu() {
      return new Promise((resolve, reject) => {
        const data: any = [
          {
            icon: 'dashboard',
            name: '控制台',
            url: '/admin/dashboard',
            childList: []
          },
          {
            icon: 'user',
            url: '/admin/product',
            name: '产品库管理',
            childList: [
              {
                icon: 'user-m',
                url: '/admin/product/classify',
                name: '产品分类',
                children: []
              },
              {
                icon: 'user-m',
                url: '/admin/product/library',
                name: '产品列表',
                children: []
              }
            ]
          },
          {
            icon: 'article-manage',
            url: '/admin/article',
            name: '文章管理',
            childList: [
              {
                icon: 'article',
                url: '/admin/article/list',
                name: '文章列表',
                children: []
              },
              {
                icon: 'article-sort',
                url: '/admin/article/sort',
                name: '文章分类',
                children: []
              },
              {
                icon: 'article-tags-filled',
                url: '/admin/article/tags',
                name: '文章标签',
                children: []
              },
              {
                icon: 'recycle',
                url: '/admin/article/recycle',
                name: '回收站',
                children: []
              }
            ]
          },
          {
            icon: 'system',
            name: '系统管理',
            url: '/admin/system',
            childList: [
              {
                icon: 'menu-filled',
                url: '/admin/system/menu',
                name: '菜单管理',
                children: []
              },
              {
                icon: 'user-m',
                url: '/admin/system/user',
                name: '用户列表',
                children: []
              },
              {
                icon: 'notice',
                url: '/admin/system/notice',
                name: '通知公告',
                children: []
              },
              {
                icon: 'message',
                url: '/admin/system/message',
                name: '留言管理',
                children: []
              },
              {
                icon: 'website',
                url: '/admin/system/website',
                name: '网站信息',
                children: []
              },
              {
                icon: 'blogroll',
                url: '/admin/system/blogroll',
                name: '友情链接',
                children: []
              }
            ]
          }
        ]
        this.menu = data
        // this.routes = filterAsyncRoutes(data)
        resolve(data)
      })
    }
  },
  // 持久化
  persist: {
    key: USER_INFO
  }
})

export default useUserStore
